define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'pms/pmsworker/index' + location.search,
                    add_url: 'pms/pmsworker/add',
                    edit_url: 'pms/pmsworker/edit',
                    del_url: 'pms/pmsworker/del',
                    multi_url: 'pms/pmsworker/multi',
                    table: 'pms_worker',
                }
            });

            var table = $("#table");

            //在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='category']", form).addClass("selectpage").data("source", "pms/pmsconfig/selectpage").data("primaryKey", "id").data("field", "name").attr('data-params', '{"key":"worker_category"}');
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,
                searchFormVisible: true,
                columns: [
                    [
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'category', title: __('Category')},
                        {field: 'realname', title: __('Realname'), operate: 'LIKE'},
                        {field: 'sex', title: __('Sex'), searchList: {"0":__('女'),"1":__('男')}, formatter: Table.api.formatter.label},
                        {field: 'birthday', title: __('Birthday'), operate:'RANGE', addclass:'datetimerange'},
                        {field: 'tel', title: __('Tel'), operate: 'LIKE'},
                        {field: 'state', title: __('State'), searchList: {"0":__('State 0'),"1":__('State 1'),"-1":__('State -1')}, formatter: Table.api.formatter.status},
                        {field: 'updatetime', title: __('Updatetime'), operate:false, addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'last_editor_nickname', title: __('Last_editor'), operate: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});